/*
 * @Author: yx
 * @Date: 2021-02-06 22:01:41
 * @LastEditTime: 2021-02-14 22:38:04
 * @LastEditors: Please set LastEditors
 * @Description: 咪咕音乐的各种接口
 * @FilePath: \Music\server\API\MIGU.js
 */
import request from './request'
/**
 * @description: 格式化歌词 
 * @param {*} lyric 主歌词
 * @param {*} lyricZh 中文歌词，默认为空
 * @return {*} 格式化后的歌词
 */
export function formatLyricsByMiGU(lyric, lyricZh = "") {
  if (!lyric.length) return {}
  var rule = /\[(ti|ar):.{0,100}\]/g,
    rule2 = /\r\n/,
    // 剔除无效行：先去头部多余信息，再分隔，给每段字符去除前后多余空格，再过滤掉无效行
    ly1 = lyric.replace(rule, '').split(rule2).map(v => v.trim()).filter(v => v.length > 11),
    natLy = ly1.join('\r\n'),
    hasZh = false
  // 若有中文版歌词
  if (!!lyricZh) {
    var ly2 = lyricZh.replace(rule, '').split(rule2).map(v => v.trim()).filter(v => v.length > 11)
    if (ly1.length === ly2.length) { // 有的翻译版和原始版句子数量对不上，如果长度相同则添加中文，否则不添加
      for (let i = ly1.length - 1, j = ly2.length - 1; i >= 0; i--, j--) {
        ly1[i] = ly1[i] + '\r\n' + ly2[j].slice(10)
      }
      hasZh = true
    }
  }
  return {
    hasZh,
    natLy,
    zhLy: ly1.join('\r\n')
  }
}

/**
 * @description: 咪咕关键词搜索
 * @param {*} keyword 关键词
 * @param {*} page 页码 1+
 * @param {*} pagesize 分页大小
 * @return {*} 
 */
export function searchFromMiGu(keyword, page, pagesize) {
  return request({
    method: 'get',
    url: '/migu/search',
    params: {
      keyword,
      page,
      pagesize
    }
  })
}

/**
 * @description: 咪咕获取歌词
 * @param {*} copyrightId
 * @return {*}
 */
export function getLyricsFromMiGu(copyrightId) {
  return request({
    method: 'get',
    url: '/migu/getLyrics',
    params: {
      copyrightId
    }
  })
}
